package com.itheima.action;

import java.sql.SQLException;

import javax.servlet.http.HttpServletRequest;

import org.apache.struts2.ServletActionContext;

import com.itheima.domain.User;
import com.itheima.service.UserService;
import com.opensymphony.xwork2.Action;
import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.util.ValueStack;

public class loginAction implements Action{
	
	public String login(){
		HttpServletRequest req = ServletActionContext.getRequest();
		String username = req.getParameter("username");
		String password = req.getParameter("password");
		User user = new User();
		user.setUsername(username);
		user.setPassword(password);
		System.out.println(username+"  "+password);
		UserService us = new UserService();
		try {
			User exitUser = us.login(user);
			if(exitUser!=null){
				req.getSession().setAttribute("exitUser", exitUser);
				return "success";
			}else{
				//用valueStack储存错误信息
				ValueStack vs = ActionContext.getContext().getValueStack();
				vs.set("msg", "用户名或者密码错误");
				return "fail";
			}
		} catch (SQLException e) {
			e.printStackTrace();
		}
		return null;
	}

	@Override
	public String execute() throws Exception {
		return null;
	}

	
}
